<?php
/**
 * oscshop 电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */
namespace Lib;

class Cart {
	
	private $data = array();

	
	//加入购物车
	public function add($goods_id, $qty = 1) {
		
		$key = (int)$goods_id;	
		
		if ((int)$qty && ((int)$qty > 0)) {			
			
			$key='cart.'.$key;
			
			$s=session($key);
			
			if (!isset($s)) {
				session($key,(int)$qty);
			} else {
				$qty=$s+(int)$qty;
				session($key,(int)$qty);			
			}		
			
		}	
		
	}	
	
	//删除商品
	public function remove($key) {
		
		$key='cart.'.$key;
		
		$s=session($key);
		
		if (isset($s)) {
			session($key,null);
		}
		
	}	
	
	//更新购物车
	public function update($goods_id, $qty) {
			
		$ckey='cart.'.$goods_id;			
		
		if ((int)$qty && ((int)$qty > 0)) {
			session($ckey,(int)$qty);			
		} else {
			$this->remove($goods_id);
		}
	
	}
	
	//获取购物车全部商品
	public function get_all_goods() {
		
		if (!($this->data)) {
			
			$cart=session('cart');			
			
			$hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
			foreach ($cart as $key => $quantity) {			
				
				$goods_id = $key;			

				$goods_query = M()->query("SELECT * FROM " . C('DB_PREFIX') . "goods p LEFT JOIN " 
				. C('DB_PREFIX') . "goods_description pd ON (p.goods_id = pd.goods_id) WHERE p.goods_id = " 
				. (int)$goods_id . " AND p.status = 1");			
				
				if ($goods_query) {						
					
					$price = $goods_query[0]['price'];						
					
					$discount=M()->query("SELECT price FROM " . C('DB_PREFIX') . "goods_discount WHERE goods_id = '" . (int)$goods_id . "' AND quantity <=" . (int)$quantity . " ORDER BY quantity DESC, price ASC LIMIT 1");
		
					if($discount){
						$price=$discount[0]['price'];
					}
					
					
					$this->data[$key] = array(
						'key'                       => $key,
						//'goods_id'                  => $goods_query[0]['goods_id'],
						'goods_id'                  =>$hashids->encode($goods_query[0]['goods_id']),
						'name'                      => $goods_query[0]['name'],
						'model'                     => $goods_query[0]['model'],
						'shipping'                  => $goods_query[0]['shipping'],						
						'image'                     => resize($goods_query[0]['image'],C('goods_cart_thumb_width'),C('goods_cart_thumb_height')),
						'quantity'                  => $quantity,
						'minimum'                   => $goods_query[0]['minimum'],
						'subtract'                  => $goods_query[0]['subtract'],						
						'price'                     =>$price,						
						'total'                     =>$price * $quantity,
						'weight'                    => $goods_query[0]['weight'] * $quantity,
						'weight_class_id'           => $goods_query[0]['weight_class_id'],
						'length'                    => $goods_query[0]['length'],
						'width'                     => $goods_query[0]['width'],
						'height'                    => $goods_query[0]['height'],
						'length_class_id'           => $goods_query[0]['length_class_id']				
					);
				
				} else {
					$this->remove($key);
				}
			}
		}
	
		return $this->data;
	}	

	//取得单个商品价格
	public function get_goods_price_by_goods_quantity($goods_id,$quantity){		
		
		$price=M('goods')->where(array('goods_id'=>$goods_id))->getField('price');	
		
		$discount=M()->query("SELECT price FROM " . C('DB_PREFIX') . "goods_discount WHERE goods_id = '" . (int)$goods_id . "' AND quantity <=" . (int)$quantity . " ORDER BY quantity DESC, price ASC LIMIT 1");
		
		if($discount){
			$price=$discount[0]['price'];
		}
		
		return round($price ,2);
		
	}
	
	//取得单个商品总价
	public function get_total_price_by_goods_id($goods_id,$quantity){
		
		$key='cart.'.$goods_id;
		
		$quantity=session($key);
		
		$price=M('goods')->where(array('goods_id'=>$goods_id))->getField('price');	
		
		$discount=M()->query("SELECT price FROM " . C('DB_PREFIX') . "goods_discount WHERE goods_id = '" . (int)$goods_id . "' AND quantity <=" . (int)$quantity . " ORDER BY quantity DESC, price ASC LIMIT 1");
		
		if($discount){
			$price=$discount[0]['price'];
		}
		return round($price*$quantity ,2);
		
	}
	//取得所有商品价格
	public function get_all_goods_price(){
		$goods_price = 0; 

		$goods = $this->get_all_goods();
		
		foreach ($goods as $goods) {
			
			$goods_price += $goods['total'];
		}		
		
		return round($goods_price,2);
	}
	
	//计算商品总数
	public function count_goods() {
		$goods_total = 0; 

		$goods = $this->get_all_goods();
		
		foreach ($goods as $goods) {
			
			$goods_total += $goods['quantity'];
		}		
		
		return $goods_total;
	}	
	//得到商品数量
	public function get_goods_quantity($goods_id){
		
		return M('goods')->where(array('goods_id'=>$goods_id))->getField('quantity');		
		
	}
	
	public function getWeight() {
		$weight = 0;
		$w=new \Lib\Weight();
		foreach ($this->get_all_goods() as $product) {
			
			if ($product['shipping']) {
				$weight += $w->convert($product['weight'], $product['weight_class_id'],C('WEIGHT_ID'));
			}
		}

		return $weight;
	}
	
	//是否需要派送,下载类商品不需要配送
	public function has_shipping() {
		$shipping = false;

		foreach ($this->get_all_goods() as $product) {
			if ($product['shipping']) {
				$shipping = true;

				break;
			}
		}

		return $shipping;
	}
	//购物车是否为空
	public function has_goods() {
		$s=session('cart');
		return count($s);
	}
}
